package com.aifund.app.controller;

import com.aifund.app.entity.form.user.BindApiForm;
import com.aifund.app.entity.form.user.IntegrationCashForm;
import com.aifund.app.entity.form.user.ModifyInformationForm;
import com.aifund.app.entity.form.user.RequestExchangeManForm;
import com.aifund.app.entity.vo.upload.UploadImgVo;
import com.aifund.app.entity.vo.user.GetInformationVo;
import com.aifund.app.entity.vo.user.MyAssetsVo;
import com.aifund.app.service.OssService;
import com.aifund.app.service.UserCashService;
import com.aifund.app.service.UserService;
import com.aifund.common.entity.AiUser;
import com.aifund.common.entity.SystemConfig;
import com.aifund.common.enums.config.ConfigEnum;
import com.aifund.common.exception.AiFundException;
import com.aifund.common.result.ResultBody;
import com.aifund.common.service.SystemConfigService;
import com.aifund.common.util.FileUtil;
import com.aifund.common.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户")
public class UserController {

    @Autowired
    RestTemplate restTemplate;


    @Autowired
    UserService userService;
    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    UserCashService userCashService;
    @Autowired
    SystemConfigService configService;
    @Autowired
    OssService ossService;

    /**
     * 上传头像服务器
     * @param request
     * @return
     */
    @PostMapping("/uploadHeader")
    @ApiOperation("上传头像")
    public ResultBody upload(MultipartHttpServletRequest request) {
        String url = configService.getByKey(ConfigEnum.FILE_SERVICE_URL.getConfigKey()).getConfigValue()+"/fileUploadPost";
        Map<String, MultipartFile> fileMap = request.getFileMap();
        MultipartFile file = null;
        if(fileMap.entrySet()==null||fileMap.entrySet().size()==0){
            throw new AiFundException("500","上传文件不能为空");
        }
        for(Map.Entry<String,MultipartFile> entry : fileMap.entrySet()){
            file = entry.getValue();
        }
        String filePath = ossService.uploadImg(file);
        return ResultBody.success(filePath);
    }

    /**
     * 获取积分
     * @return
     */
    @GetMapping("/getIntegration")
    @ApiOperation("获取积分")
    public ResultBody<BigDecimal> getIntegration(HttpServletRequest request){
        BigDecimal integration = userService.getIntegration(jwtUtil.getUserId(request));
        return ResultBody.success(integration);
    }

    /**
     * 用户提币
     * @param cashForm
     * @return
     */
    @PostMapping("/integrationCash")
    @ApiOperation("用户提币")
    public ResultBody integrationCash(@RequestBody @Validated IntegrationCashForm cashForm,HttpServletRequest request){
        userService.integrationCash(cashForm,jwtUtil.getUserId(request));
        return ResultBody.success();
    }

    /**
     * 绑定API
     * @param bindApiForm
     * @return
     */
    @PostMapping("/bindApi")
    @ApiOperation("绑定API")
    public ResultBody bindApi(@RequestBody @Validated BindApiForm bindApiForm,HttpServletRequest request){
        userService.bindApi(bindApiForm,jwtUtil.getUserId(request));
        return ResultBody.success();
    }

    /**
     * 修改资料
     * @param form
     * @param request
     * @return
     */
    @ApiOperation("修改资料")
    @PostMapping("/modifyInformation")
    public ResultBody modifyInformation(@Validated @RequestBody ModifyInformationForm form,HttpServletRequest request){
        userService.modifyInformation(form,jwtUtil.getUserId(request));
        return ResultBody.success();
    }

    /**
     * 获取资料
     * @param request
     * @return
     */
    @ApiOperation("获取资料")
    @GetMapping("/getInformation")
    public ResultBody<GetInformationVo> getInformation(HttpServletRequest request){
        AiUser aiUser = userService.getById(jwtUtil.getUserId(request));
        GetInformationVo vo = new GetInformationVo();
        BeanUtils.copyProperties(aiUser,vo);
        if(aiUser.getPayPassword() != null && !aiUser.getPayPassword().isEmpty()){
            vo.setSetPayPassword(true);
        }else{
            vo.setSetPayPassword(false);
        }
        return ResultBody.success(vo);
    }

    /**
     * 充值
     * @param request
     * @return
     */
    @PostMapping("/charge")
    @ApiOperation("充值")
    public ResultBody charge(MultipartHttpServletRequest request){
        Map<String, MultipartFile> fileMap = request.getFileMap();
        MultipartFile file = null;
        if(fileMap.entrySet()==null||fileMap.entrySet().size()==0){
            throw new AiFundException("500","上传文件不能为空");
        }
        for(Map.Entry<String,MultipartFile> entry : fileMap.entrySet()){
            file = entry.getValue();
        }
        String path = ossService.uploadImg(file);
        String chargeNum = request.getParameter("chargeNum");
        userService.charge(path,new BigDecimal(chargeNum),jwtUtil.getUserId(request));
        return ResultBody.success();
    }


    @PostMapping("/requestExchangeMan")
    @ApiOperation("申请交易员")
    public ResultBody requestExchangeMan(@RequestBody @Validated RequestExchangeManForm form,HttpServletRequest request){
        userService.requestExchangeMan(form,jwtUtil.getUserId(request));
        return ResultBody.success();
    }


    /**
     * 我的资产
     * @return
     */
    @GetMapping("/myAssets")
    @ApiOperation("我的资产")
    public ResultBody<MyAssetsVo> myAssets(HttpServletRequest request){
        return ResultBody.success(userService.myAssets(jwtUtil.getUserId(request), 1L));
    }


    /**
     * 更新是否是第一次打开
     * @param request
     * @return
     */
    @PostMapping("/updateOpenFirst")
    @ApiOperation("更新是否是第一次打开状态")
    public ResultBody updateOpenFirst(HttpServletRequest request){
        userService.updateFirstOpen(jwtUtil.getUserId(request));
        return ResultBody.success();
    }

}
